﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FunMazeEngine
{
    [Serializable]
    public class Game: MarshalByRefObject
    {
        public List<IGameClient> gamePlayerList;

        public List<GamePlayer> playerList;

        public List<int> occupiedlist = new List<int>();

        public Game()
        {
            gamePlayerList = new List<IGameClient>();
            playerList = new List<GamePlayer>();
        }

        public string GameName { get; set; }
        public Map GameMap { get; set; }

        public List<IGameClient> PlayerList { get { return gamePlayerList; } set { gamePlayerList = value; } }

        public List<GamePlayer> PlayerEntityList { get { return playerList; } set { playerList = value; } }

        public bool AddPlayer(IGameClient thePlayer)
        {
            //if(!gamePlayerList.Exists(e=> e.Player.Name == thePlayer.Player.Name))
            {
                gamePlayerList.Add(thePlayer);
                return true;
            }
            //else
            //{
            //    return false;
            //}
            
        }

        public void RemovePlayer(IGameClient thePlayer)
        {
            gamePlayerList.Remove(thePlayer);
        }

        public bool NameChecker(string thePlayer)
        {
            bool nameexists = false;
            foreach (IGameClient client in gamePlayerList) 
            {
                if (thePlayer == client.Player.Name ) { nameexists = true; }
            }
            return nameexists;
        }

        public IGameClient RetrieveGamePlayer(string name)
        {
            IGameClient result = null;
            foreach (IGameClient client in gamePlayerList)
            {
                if (client.Player.Name == name) { result = client; }
            }
            return result;
        }

        public bool PlayerThere(int cellnumber)
        {
            bool exists = false;
            if (occupiedlist.Contains(cellnumber))
            {
                exists = true;
            }
            /*
            foreach (IGameClient client in gamePlayerList)  {
                if (client.Player.Location == cellnumber)
                {
                     exists = true; // if someone is there, then it is true
                }
            }*/
            return exists;
        }

        public string BackUpServerAddress { get; set; }
      
    }
}
